﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace WebUI.Pages
{
    public partial class editUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get credentials
            UserController usrController = new UserController();
            int secUsuario = usrController.GetUserID(User.Identity.Name);
            int secTeam = usrController.GetUserTeamID(secUsuario);

            if (secTeam != 9)
            {
                Response.Redirect("../Pages/403.aspx");
            }            
            
            if (!this.IsPostBack)
            {
                TeamController tmController = new TeamController();

                ddlGestor.DataSource = usrController.GetTeamUsers(9);
                ddlTime.DataSource = tmController.GetTeams();
                ddlGestor.DataBind();
                ddlTime.DataBind();    

                ddlGestor.DataValueField = "idUsuario";
                ddlTime.DataValueField = "idTime";

                ddlGestor.DataTextField = "Nome";
                ddlTime.DataTextField = "DescTime";

                ddlGestor.DataBind();
                ddlTime.DataBind();    
    
                int idUsuario = Convert.ToInt32(Request.QueryString["id"]);
                int idGestor = usrController.GetGestorID(idUsuario);
                int idTime = usrController.GetUserTeamID(idUsuario);

                lblID.Text = idUsuario.ToString();

                ddlGestor.SelectedValue = idGestor.ToString();
                ddlTime.SelectedValue = idTime.ToString();

                txtNome.Text = usrController.GetUserName(idUsuario).ToString();
                txtLogin.Text = usrController.GetUserLogin(idUsuario).ToString();
            }
        }

        protected void cmdSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                UserController usrController = new UserController();

                int idUsuario = Convert.ToInt32(lblID.Text);
                int idGestor = Convert.ToInt32(ddlGestor.SelectedValue);
                int idTime = Convert.ToInt32(ddlTime.SelectedValue);

                string Login = txtLogin.Text;
                string Nome = txtNome.Text;

                usrController.UpdateUser(idUsuario, idTime, idGestor, Login, Nome);
            }
            catch (Exception ex)
            {
                lblMsg.Text = "Erro ao atualizar usuário. " + ex.Data;
            }
            finally
            {
            }
        }
    }
}